﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
using BaocaoNPC.common;
using Component;
using CMISLibrary;
using CMISLibrary.Libraries;
using System.IO;
using Microsoft.VisualBasic;
using BaocaoNPC.Usercontrols.reports;
using bus_TienichNPC;


namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for InBaoCaoTHEODOINO.xaml
    /// </summary>
    public partial class InBaoCaoTHEODOINO : UserControl
    {
        #region Variables
        //NPC_CHAMNO_OFFLINE_bus CHAMNO_OFFLINE_bus = new NPC_CHAMNO_OFFLINE_bus();
        //CN_THEODOINO_bus THEODOINO_bus = new CN_THEODOINO_bus();
        public String strMadviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public string strUsername = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "NPC_ATM";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
        private DataSet ds_TNV = new DataSet();
        DataSet dsResult = new DataSet();
        public Int16 i16ThangHT = 0;
        public Int16 i16NamHT = 0;
        public int daysInMonth_HT = 1;
        int IN = 1;
        DataSet ds_ketqua = new BCNPCEntities.EXCEL.ds_CN();
        DataSet ds_cn = new BCNPCEntities.EXCEL.ds_CN();
        #endregion

        public InBaoCaoTHEODOINO()
        {
            InitializeComponent();
            Component.TabIndexManager.RegisterTabManagerWithSelectAll(this);
            GET_THANGNAM_HT();
            GetD_DVI_QLY();
            dtptungay.SelectedDate = DateTime.Now.Date;
            dtpdenngay.SelectedDate = DateTime.Now.Date;
            dtpThangHTtim.Text = "0" + i16ThangHT.ToString() + "/" + i16NamHT.ToString();
            LOAD_DATA();
            Update_autotext();
            grdLich.IsEnabled = false;  
        }

        private void GET_THANGNAM_HT()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "CN" && dr["MA_DVIQLY"].ToString() == strMadviqly)
                        {
                            i16ThangHT = Convert.ToInt16(dr["THANG"]);
                            i16NamHT = Convert.ToInt16(dr["NAM"]);
                        }
                }
            }
            catch
            {
                i16ThangHT = Convert.ToInt16(DateTime.Now.Month);
                i16NamHT = Convert.ToInt16(DateTime.Now.Year);
            }
        }

        private void GetD_DVI_QLY()
        {
            try
            {
                DataSet dsD_DVI_QLY = new DataSet();

                //cls_TienichNPC_bus cls = new cls_TienichNPC_bus();

                //dsD_DVI_QLY = cls.select_D_DVI_QLY_ALL(strMa_dviqly);

                dsD_DVI_QLY = GlobalServices.Service_NPCUS.select_D_DVI_QLY_ALL(strMadviqly);

                if (dsD_DVI_QLY == null)
                {
                    System.Windows.MessageBox.Show("Không lấy được danh mục đơn vị quản lý.");
                }
                else
                {
                    DataView dv = dsD_DVI_QLY.Tables["D_DVI_QLY"].DefaultView;
                    cboDonVi.ItemsSource = dv;
                    cboDonVi.DisplayMemberPath = "TEN_DVIQLY";
                    cboDonVi.SelectedValuePath = "MA_DVIQLY";
                    cboDonVi.SelectedIndex = 0;
                }
                if (strMadviqly.Length == 6)
                { cboDonVi.IsEnabled = false; }
                else
                { cboDonVi.IsEnabled = true; }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        public void showErrorMess(string s)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 5000, s, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
        }

        public void showSucrMess(string s)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 5000, s, Component.Validate.ValidationManager.MESSEGER_TYPE.SUCCESS_MESSAGE);
        }

        private void LOAD_DATA()
        {
            try
            {
                dtpThangHTtim.IsEnabled = false;
                ds_TNV = GlobalServices.Service_NPCUS.SELECT_TNGAN_ALL(strMadviqly);
                fill_dulieu(ds_TNV);
            }
            catch(Exception ex)
            { }
        }

        private void fill_dulieu(DataSet ds)
        {
            try
            {
                if (ds.Tables[0].Rows.Count == 0 || ds == null) return;
                TreeViewItem superNode = new TreeViewItem();
                TreeViewItem _node = new TreeViewItem();
                TreeViewItem _childnode = new TreeViewItem();
                CheckBox chkNode = new CheckBox();
                CheckBox chkChildNode = new CheckBox();

                if (trvBBAN != null) trvBBAN.Items.Clear();
                DataView dv = new DataView(ds.Tables[0]);
                dv.Sort = "MA_TNGAN";
                if (dv.Count == 0)
                {
                    superNode.Header = "Không có dữ liệu TNV";
                    superNode.Tag = "Không có dữ liệu TNV@";
                    trvBBAN.Items.Add(superNode);
                    return;
                }
                CheckBox chkSuperNode = new CheckBox();
                chkSuperNode.Content = strTen_dviqly;
                chkSuperNode.Tag = "NUTGOC" + "@" + "" + "@" + "" + "@" + "" + "@" + "";
                chkSuperNode.ToolTip = strTen_dviqly;
                chkSuperNode.IsChecked = false;
                chkSuperNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                superNode = new TreeViewItem();
                superNode.Header = chkSuperNode;
                superNode.IsExpanded = true;
                //trvBBAN.Items.Add(superNode);
                string strnutcha = "";
                string strNutCon = "";
                string str_thungan = "";
                string str_ma_thungan = "";
                foreach (DataRowView dr in dv)
                {
                    str_ma_thungan = dr["MA_TNGAN"].ToString();
                    str_thungan = dr["TEN_TNGAN"].ToString();
                    #region "parentnode"
                    if (str_thungan != strnutcha)
                    {
                        chkNode = new CheckBox();
                        chkNode.Content = str_thungan;
                        chkNode.Tag = str_ma_thungan + "@NUTCHA" + "@" + "" + "@" + "" + "@" + "";
                        chkNode.ToolTip = str_thungan;
                        chkNode.IsChecked = false;
                        chkNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                        _node = new TreeViewItem();
                        _node.Header = chkNode;
                        _node.IsExpanded = false;
                        trvBBAN.Items.Add(_node);
                        strnutcha = str_thungan;
                        #region SoBBG
                        //if (IN != 1)
                        //{
                        //    DataSet dsBBG = new DataSet();
                        //    dsBBG = CHAMNO_OFFLINE_bus.DS_BBG(str_ma_thungan, strMadviqly);
                        //    if (dsBBG != null && dsBBG.Tables[0].Rows.Count > 0)
                        //    {
                        //        if (dsBBG.Tables.Count > 0 && dsBBG.Tables[0].Rows.Count > 0)
                        //        {
                        //            TreeViewItem superNodeBB = new TreeViewItem();
                        //            TreeViewItem _nodeBB = new TreeViewItem();
                        //            //TreeViewItem _childnodeBB = new TreeViewItem();
                        //            //CheckBox chkNodeBB = new CheckBox();
                        //            CheckBox chkChildNodeBB = new CheckBox();

                        //            // filter_treeDataSet();
                        //            //this._node.Items.Clear(); // xoa tree trước khi đỗ dữ liệu mới
                        //            DataView dv2 = new DataView(dsBBG.Tables[0]);
                        //            dv2.Sort = "SO_BBGIAO, MA_SOGCS";
                        //            string strnutcha2 = "";
                        //            string str_bienban2 = "";
                        //            string str_ma_sogcs = "";
                        //            foreach (DataRowView dr2 in dv2)
                        //            {
                        //                str_bienban2 = dr2["SO_BBGIAO"].ToString();
                        //                str_ma_sogcs = dr2["MA_SOGCS"].ToString();
                        //                //str_ma_sogcs = dr2["MA_SOGCS"].ToString();
                        //                if (IN != 1)
                        //                {
                        //                    #region "parentnode2"
                        //                    if (str_bienban2 != strnutcha2)
                        //                    {
                        //                        CheckBox chkNodeBB = new CheckBox();
                        //                        chkNodeBB.Content = str_bienban2;
                        //                        chkNodeBB.Tag = str_bienban2 + "@" + str_ma_thungan + "@" + "Biên bản" + "@NUTCHA2" + "@" + "";
                        //                        chkNodeBB.ToolTip = str_bienban2;
                        //                        chkNodeBB.IsChecked = false;
                        //                        chkNodeBB.Click += new RoutedEventHandler(this.CheckBox_Click);
                        //                        _nodeBB = new TreeViewItem();
                        //                        _nodeBB.Header = chkNodeBB;
                        //                        _nodeBB.IsExpanded = false;
                        //                        _node.Items.Add(_nodeBB);
                        //                        strnutcha2 = str_bienban2;


                        //                    }
                        //                    # endregion
                        //                    #region Ma So GCS

                        //                    TreeViewItem _nodeSo = new TreeViewItem();
                        //                    CheckBox chkChildNodeSo = new CheckBox();
                        //                    if (str_ma_sogcs != strNutCon)
                        //                    {
                        //                        CheckBox chkNodeSo = new CheckBox();
                        //                        chkNodeSo.Content = str_ma_sogcs;
                        //                        chkNodeSo.Tag = str_ma_sogcs + "@" + str_bienban2 + "@" + str_ma_thungan + "@" + "Sổ" + "@NUTCON";
                        //                        chkNodeSo.ToolTip = str_ma_sogcs;
                        //                        chkNodeSo.IsChecked = false;
                        //                        chkNodeSo.Click += new RoutedEventHandler(this.CheckBox_Click);
                        //                        _nodeSo = new TreeViewItem();
                        //                        _nodeSo.Header = chkNodeSo;
                        //                        _nodeSo.IsExpanded = false;
                        //                        _nodeBB.Items.Add(_nodeSo);
                        //                        strNutCon = str_ma_sogcs;
                        //                    }
                        //                    #endregion
                        //                }
                        //            }
                        //        }
                        //    }
                        //}
                        #endregion
                    }
                    # endregion
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }

        public void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ds_ketqua.Tables["NPC_CN"].Rows.Count > 15000)
                {
                    MessageBox.Show("Dữ liệu quá lớn!");
                    return;
                }
                if (IN == 1)
                {
                    #region IN == 1
                    string ma_tngan = strUsername.ToUpper();
                    CheckBox chk = (CheckBox)sender;
                    this.Cursor = Cursors.Wait;
                    TreeViewItem itemselect = trvBBAN.SelectedItem as TreeViewItem;
                    string[] m = chk.Tag.ToString().Split('@');
                    if (chk.IsChecked == true)
                    {
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            checkList(supernode, chk);
                        }
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            RefreshCheckBoxList(supernode);
                        }
                        if (m[0] == "NUTGOC")
                        {
                            #region NUTGOC
                            MessageBox.Show("Dữ liệu quá lớn! đảo chọn và chọn theo từng thu ngân");
                            //ds_ketqua.Tables["NPC_CN"].Rows.Clear();
                            //dsResult = THEODOINO_bus.SELECT_CHAMNO_OFFLINE_ALL(strMadviqly);
                            //int rowscount = dsResult.Tables[0].Rows.Count;
                            //if (rowscount > 15000)
                            //{
                            //    MessageBox.Show("Dữ liệu quá lớn!");
                            //    this.Cursor = Cursors.Arrow;
                            //    return;
                            //}                            
                            //if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            //{
                            //    DataRow[] rgINS = dsResult.Tables[0].Select();
                            //    for (int i = 0; i < rgINS.Length; i++)
                            //    {
                            //        DataRow dr = ds_ketqua.Tables["NPC_CN"].NewRow();
                            //        dr["ID_HDON"] = rgINS[i]["ID_HDON"];
                            //        dr["TIEN_NOP"] = rgINS[i]["TIEN_NO"];
                            //        dr["THUE_NOP"] = rgINS[i]["THUE_NO"];
                            //        dr["SO_BBGIAO"] = rgINS[i]["SO_BBGIAO"];
                            //        dr["NGAY_CHAMNO"] = DBNull.Value;
                            //        dr["THANG_HT"] = 1;
                            //        dr["TTIEN"] = rgINS[i]["TONG_TIEN"];
                            //        dr["MA_TNGAN"] = null;
                            //        dr["MA_SOGCS"] = rgINS[i]["MA_SOGCS"];
                            //        dr["MA_KHANG"] = rgINS[i]["MA_KHANG"];
                            //        dr["TEN_KHANG"] = rgINS[i]["TEN_KHANG"];
                            //        dr["DCHI_KHANG"] = rgINS[i]["DCHI_KHANG"];
                            //        dr["KY"] = rgINS[i]["KY"];
                            //        dr["THANG"] = rgINS[i]["THANG"];
                            //        dr["NAM"] = rgINS[i]["NAM"];
                            //        ds_ketqua.Tables["NPC_CN"].Rows.Add(dr);
                            //        ds_ketqua.Tables["NPC_CN"].AcceptChanges();
                            //    }
                            //    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                            //}
                            #endregion
                        }
                        if (m[1].ToString() == "NUTCHA")
                        {
                            #region NUTCHA
                            if (ds_ketqua.Tables["NPC_CN"].Rows.Count > 15000)
                            {
                                MessageBox.Show("Dữ liệu quá lớn!");
                                return;
                            }
                            dsResult = GlobalServices.Service_NPCUS.SELECT_HDON(strMadviqly, m[0]); 
                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                {
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                                }
                                else
                                {
                                    for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                    {
                                        if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                        {
                                            ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                        }
                                    }
                                    ds_ketqua.AcceptChanges();
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        #region Uncheck
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            UncheckList(supernode, chk);
                        }
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            RefreshCheckBoxList(supernode);
                        }
                        if (m[0] == "NUTGOC")
                        {
                            ds_ketqua.Tables["NPC_CN"].Rows.Clear();
                        }
                        if (m[1].ToString() == "NUTCHA")
                        {
                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                        }
                        if (m[3].ToString() == "NUTCHA2")
                        {
                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["SO_BBGIAO"].ToString() == m[1].ToString())
                                {
                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                        }
                        if (m[4].ToString() == "NUTCON")
                        {
                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_SOGCS"].ToString() == m[2].ToString())
                                {
                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                        }
                        #endregion
                    }
                    #endregion
                }
                if (IN == 2)
                {
                    #region IN == 2
                    string ma_tngan = strUsername;
                    CheckBox chk = (CheckBox)sender;
                    this.Cursor = Cursors.Wait;
                    TreeViewItem itemselect = trvBBAN.SelectedItem as TreeViewItem;
                    string[] m = chk.Tag.ToString().Split('@');
                    if (chk.IsChecked == true)
                    {
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            checkList(supernode, chk);
                        }
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            RefreshCheckBoxList(supernode);
                        }
                        if (m[0] == "NUTGOC")
                        {
                            #region NUTGOC
                            MessageBox.Show("Dữ liệu quá lớn! đảo chọn và chọn theo từng thu ngân");
                            //ds_ketqua.Tables["NPC_CN"].Rows.Clear();
                            //dsResult = THEODOINO_bus.SELECT_CHAMNO_OFFLINE_ALL(strMadviqly);
                            //int count = dsResult.Tables[0].Rows.Count;
                            //if (dsResult.Tables[0].Rows.Count > 15000)
                            //{
                            //    MessageBox.Show("Dữ liệu quá lớn!");
                            //    return;
                            //}
                            //if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            //{
                            //    DataRow[] rgINS = dsResult.Tables[0].Select();
                            //    for (int i = 0; i < rgINS.Length; i++)
                            //    {
                            //        DataRow dr = ds_ketqua.Tables["NPC_CN"].NewRow();
                            //        dr["ID_HDON"] = rgINS[i]["ID_HDON"];
                            //        dr["TIEN_NOP"] = rgINS[i]["TIEN_NO"];
                            //        dr["THUE_NOP"] = rgINS[i]["THUE_NO"];
                            //        dr["SO_BBGIAO"] = rgINS[i]["SO_BBGIAO"];
                            //        dr["NGAY_CHAMNO"] = rgINS[i]["NGAY_CHAMNO"];
                            //        dr["THANG_HT"] = dtpThangHTtim.SelectedDate.Value.Month;
                            //        dr["TTIEN"] = rgINS[i]["TONG_TIEN"];
                            //        dr["MA_TNGAN"] = rgINS[i]["MA_TNGAN"];
                            //        dr["MA_SOGCS"] = rgINS[i]["MA_SOGCS"];
                            //        dr["MA_KHANG"] = rgINS[i]["MA_KHANG"];
                            //        dr["TEN_KHANG"] = rgINS[i]["TEN_KHANG"];
                            //        dr["DCHI_KHANG"] = rgINS[i]["DCHI_KHANG"];
                            //        dr["KY"] = rgINS[i]["KY"];
                            //        dr["THANG"] = rgINS[i]["THANG"];
                            //        dr["NAM"] = rgINS[i]["NAM"];
                            //        ds_ketqua.Tables["NPC_CN"].Rows.Add(dr);
                            //        ds_ketqua.Tables["NPC_CN"].AcceptChanges();
                            //    }
                            //    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                            //}
                            #endregion
                        }                    
                        if (m[1].ToString() == "NUTCHA")
                        {
                            #region NUTCHA
                            if (ds_ketqua.Tables["NPC_CN"].Rows.Count > 15000)
                            {
                                MessageBox.Show("Dữ liệu quá lớn!");
                                return;
                            }
                            dsResult = GlobalServices.Service_NPCUS.SELECT_CHAMNO_OFFLINE_NUTCHA(strMadviqly, m[0]);
                            if (dsResult.Tables[0].Rows.Count == 0) return;
                            int rows = dsResult.Tables[0].Rows.Count;
                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                    {
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                                    }
                                    else
                                    {
                                        for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                        {
                                            if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                            {
                                                ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                            }
                                        }
                                        ds_ketqua.AcceptChanges();
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[2]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                                    }
                                }

                            }
                            #endregion
                        }

                        else if (m[3].ToString() == "NUTCHA2")
                        {
                            #region NUT SO_BBGIAO 
                            dsResult = GlobalServices.Service_NPCUS.SELECT_CHAMNO_OFFLINE_NUTSO_BBG(strMadviqly,m[1], Convert.ToInt32(m[0])); 

                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                    {
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[1]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                                    }
                                    else
                                    {
                                        for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                        {
                                            if (ds_ketqua.Tables["NPC_CN"].Rows[i]["SO_BBGIAO"].ToString() == m[0].ToString())
                                            {
                                                ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                            }
                                        }
                                        ds_ketqua.AcceptChanges();
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[2]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                                    }
                                }
                            }
                            #endregion
                        }
                        else if (m[4].ToString() == "NUTCON")
                        {
                            #region NUT SO_GCS
                            // lấy dữ liệu của nút con   
                            dsResult = GlobalServices.Service_NPCUS.SELECT_CHAMNO_OFFLINE_NUTSO_GCS(strMadviqly, m[2], Convert.ToInt32(m[1]), m[0] ); 

                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                    {
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[2]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                                    }
                                    else
                                    {
                                        for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                        {
                                            if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_SOGCS"].ToString() == m[0].ToString())
                                            {
                                                ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                            }
                                        }
                                        ds_ketqua.AcceptChanges();
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[2]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                                    }
                                }
                            }

                            #endregion
                        }
                    }
                    else
                    {
                        #region Uncheck
                        //DataRow dr =null;
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            UncheckList(supernode, chk);
                        }
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            RefreshCheckBoxList(supernode);
                        }
                        if (m[0] == "NUTGOC")
                        {
                            ds_ketqua.Tables["NPC_CN"].Rows.Clear();
                        }
                        // nếu click nút cha
                        if (m[1].ToString() == "NUTCHA")
                        {
                            #region Uncheck NUTCHA
                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                            #endregion
                        }
                        if (m[3].ToString() == "NUTCHA2")
                        {
                            #region Uncheck NUTCHA2
                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["SO_BBGIAO"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                            #endregion
                        }
                        if (m[4].ToString() == "NUTCON")
                        {
                            #region Uncheck NUTCON
                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_SOGCS"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                if (IN == 3)
                {
                    #region IN == 3
                    string ma_tngan = strUsername;
                    CheckBox chk = (CheckBox)sender;
                    this.Cursor = Cursors.Wait;
                    TreeViewItem itemselect = trvBBAN.SelectedItem as TreeViewItem;
                    // cắt mảng tag
                    string[] m = chk.Tag.ToString().Split('@');
                    if (chk.IsChecked == true)
                    {
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            checkList(supernode, chk);
                        }
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            RefreshCheckBoxList(supernode);
                        }
                        // nếu click nút tổng
                        if (m[0] == "NUTGOC")
                        {
                            #region NUTGOC
                            MessageBox.Show("Dữ liệu quá lớn! đảo chọn và chọn theo từng thu ngân");
                            //ds_ketqua.Tables["NPC_CN"].Rows.Clear();
                            //dsResult = THEODOINO_bus.SELECT_ATM_NUTGOC(strMadviqly); 
                            //if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            //{
                            //    DataRow[] rgINS = dsResult.Tables[0].Select();
                            //    for (int i = 0; i < rgINS.Length; i++)
                            //    {
                            //        DataRow dr = ds_ketqua.Tables["NPC_CN"].NewRow();
                            //        dr["ID_HDON"] = rgINS[i]["ID_HDON"];
                            //        dr["TIEN_NOP"] = rgINS[i]["TIEN_NO"];
                            //        dr["THUE_NOP"] = rgINS[i]["THUE_NO"];
                            //        dr["SO_BBGIAO"] = rgINS[i]["SO_BBGIAO"];
                            //        dr["NGAY_CHAMNO"] = rgINS[i]["NGAY_TTOAN"];
                            //        dr["TTIEN"] = rgINS[i]["TONGTIEN"];
                            //        dr["MA_TNGAN"] = null;
                            //        dr["MA_SOGCS"] = rgINS[i]["MA_SOGCS"];
                            //        dr["MA_KHANG"] = rgINS[i]["MA_KHANG"];
                            //        dr["TEN_KHANG"] = rgINS[i]["TEN_KHANG"];
                            //        dr["DCHI_KHANG"] = rgINS[i]["DCHI_KHANG"];
                            //        dr["KY"] = rgINS[i]["KY"];
                            //        dr["THANG"] = rgINS[i]["THANG"];
                            //        dr["NAM"] = rgINS[i]["NAM"];
                            //        ds_ketqua.Tables["NPC_CN"].Rows.Add(dr);
                            //        ds_ketqua.Tables["NPC_CN"].AcceptChanges();
                            //    }
                            //    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                            //}
                            #endregion
                        }

                        // nếu click nút cha                        
                        if (m[1].ToString() == "NUTCHA")
                        {
                            #region NUT THU NGAN
                            // lấy dữ liệu của nút cha                               
                            dsResult = GlobalServices.Service_NPCUS.SELECT_ATM_NUT_TNGAN(strMadviqly, m[0]); 
                            
                            if (dsResult != null && dsResult.Tables[0].Rows.Count >0)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                {
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                                else
                                {
                                    for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                    {
                                        if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                        {
                                            ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                        }
                                    }
                                    ds_ketqua.AcceptChanges();
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                            }
                            #endregion
                        }

                        else if (m[3].ToString() == "NUTCHA2")
                        {
                            #region NUT SO BBGIAO
                            // lấy dữ liệu của nút cha2   
                            dsResult = GlobalServices.Service_NPCUS.SELECT_ATM_NUT_SOBBGIAO(strMadviqly,m[1], Convert.ToInt32(m[0])); 
                            
                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                {
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                                else
                                {
                                    for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                    {
                                        if (ds_ketqua.Tables["NPC_CN"].Rows[i]["SO_BBGIAO"].ToString() == m[0].ToString())
                                        {
                                            ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                        }
                                    }
                                    ds_ketqua.AcceptChanges();
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                            }
                            #endregion
                        }
                        else if (m[4].ToString() == "NUTCON")
                        {
                            #region NUT MA_SOGCS
                            // lấy dữ liệu của nút con   
                            dsResult = GlobalServices.Service_NPCUS.SELECT_ATM_NUT_MASOGCS(strMadviqly, m[2], Convert.ToInt32(m[1]), m[0]);

                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                {
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                                else
                                {
                                    for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                    {
                                        if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_SOGCS"].ToString() == m[0].ToString())
                                        {
                                            ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                        }
                                    }
                                    ds_ketqua.AcceptChanges();
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                            }

                            #endregion
                        }
                    }
                    else
                    {
                        #region Uncheck
                        //DataRow dr =null;
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            UncheckList(supernode, chk);
                        }
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            RefreshCheckBoxList(supernode);
                        }
                        if (m[0] == "NUTGOC")
                        {
                            ds_ketqua.Tables["NPC_CN"].Rows.Clear();
                        }
                        // nếu click nút cha
                        if (m[1].ToString() == "NUTCHA")
                        {
                            #region Uncheck NUTCHA
                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                            #endregion
                        }
                        if (m[3].ToString() == "NUTCHA2")
                        {
                            #region Uncheck NUTCHA2
                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["SO_BBGIAO"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                            #endregion
                        }
                        if (m[4].ToString() == "NUTCON")
                        {
                            #region Uncheck NUTCON
                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_SOGCS"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                if (IN == 4)
                {
                    #region IN == 4
                    string ma_tngan = strUsername;
                    CheckBox chk = (CheckBox)sender;
                    this.Cursor = Cursors.Wait;
                    //chk.Cursor = Cursors.Wait;
                    //lblstatus.Content = "Đang lấy dữ liệu hóa đơn...";
                    TreeViewItem itemselect = trvBBAN.SelectedItem as TreeViewItem;
                    // cắt mảng tag
                    string[] m = chk.Tag.ToString().Split('@');
                    if (chk.IsChecked == true)
                    {
                        //kiem tra xem root or leaf duoc check
                        //#region Check All ChildNode
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            checkList(supernode, chk);
                        }
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            RefreshCheckBoxList(supernode);
                        }
                        // nếu click nút tổng
                        if (m[0] == "NUTGOC")
                        {
                            #region NUTGOC
                            MessageBox.Show("Dữ liệu quá lớn! đảo chọn và chọn theo từng thu ngân");
                            //ds_ketqua.Tables["CN_TON"].Rows.Clear();
                            //dsResult = THEODOINO_bus(strMadviqly); 
                            
                            //if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            //{
                            //    DataRow[] rgINS = dsResult.Tables[0].Select();
                            //    for (int i = 0; i < rgINS.Length; i++)
                            //    {
                            //        DataRow dr = ds_ketqua.Tables["CN_TON"].NewRow();
                            //        dr["ID_HDON"] = rgINS[i]["ID_HDON"];
                            //        dr["TEN_KHANG"] = rgINS[i]["TEN_KHANG"];
                            //        dr["DCHI_KHANG"] = rgINS[i]["DCHI_KHANG"];
                            //        dr["MA_KHANG"] = rgINS[i]["MA_KHANG"];
                            //        dr["TIEN_NO"] = rgINS[i]["TIEN_NO"];
                            //        dr["THUE_NO"] = rgINS[i]["THUE_NO"];
                            //        dr["TONG_TIEN"] = rgINS[i]["TONG_TIEN"];
                            //        dr["SO_BBGIAO"] = rgINS[i]["SO_BBGIAO"];
                            //        dr["MA_SOGCS"] = rgINS[i]["MA_SOGCS"];
                            //        dr["KY"] = rgINS[i]["KY"];
                            //        dr["THANG"] = rgINS[i]["THANG"];
                            //        dr["NAM"] = rgINS[i]["NAM"];
                            //        dr["MA_TNGAN"] = null;
                            //        ds_ketqua.Tables["CN_TON"].Rows.Add(dr);
                            //        ds_ketqua.AcceptChanges();
                            //    }
                            //    ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";

                            //}
                            #endregion
                        }

                        // nếu click nút cha                        
                        if (m[1].ToString() == "NUTCHA")
                        {
                            #region NUTCHA
                            // lấy dữ liệu của nút cha                               
                            dsResult = GlobalServices.Service_NPCUS.SELECT_HDON_TON_NUTTNGAN(strMadviqly, m[0]);
                            int count = dsResult.Tables[0].Rows.Count;
                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (ds_ketqua.Tables["CN_TON"].Rows.Count == 0)
                                {
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[0]);
                                    ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                }
                                else
                                {
                                    for (int i = ds_ketqua.Tables["CN_TON"].Rows.Count - 1; i >= 0; i--)
                                    {
                                        if (ds_ketqua.Tables["CN_TON"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                        {
                                            ds_ketqua.Tables["CN_TON"].Rows[i].Delete();
                                        }
                                    }
                                    ds_ketqua.AcceptChanges();
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[0]);
                                    ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                }
                            }
                            #endregion
                        }

                        else if (m[3].ToString() == "NUTCHA2")
                        {

                            #region NUTCHA2
                            try
                            {
                                // lấy dữ liệu của nút cha2   
                                dsResult = GlobalServices.Service_NPCUS.DSTon_Nutcha2(m[1], Convert.ToInt64(m[0]), strMadviqly); 

                                if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (dsResult.Tables[0].Rows.Count > 0)
                                    {
                                        if (ds_ketqua.Tables["CN_TON"].Rows.Count == 0)
                                        {
                                            copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[1]);
                                            ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                        }
                                        else
                                        {
                                            for (int i = ds_ketqua.Tables["CN_TON"].Rows.Count - 1; i >= 0; i--)
                                            {
                                                if (ds_ketqua.Tables["CN_TON"].Rows[i]["SO_BBGIAO"].ToString() == m[0].ToString())
                                                {
                                                    ds_ketqua.Tables["CN_TON"].Rows[i].Delete();
                                                }
                                            }
                                            ds_ketqua.AcceptChanges();
                                            copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[1]);
                                            ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                            #endregion
                        }
                        else if (m[4].ToString() == "NUTCON")
                        {
                            #region NUTCON
                            try
                            {
                                // lấy dữ liệu của nút con   
                                dsResult = GlobalServices.Service_NPCUS.DSTon_Nutcon(m[2], Convert.ToInt64(m[1]), m[0], strMadviqly);

                                if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (dsResult.Tables[0].Rows.Count > 0)
                                    {
                                        if (ds_ketqua.Tables["CN_TON"].Rows.Count == 0)
                                        {
                                            copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[2]);
                                            ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                        }
                                        else
                                        {
                                            for (int i = ds_ketqua.Tables["CN_TON"].Rows.Count - 1; i >= 0; i--)
                                            {
                                                if (ds_ketqua.Tables["CN_TON"].Rows[i]["MA_SOGCS"].ToString() == m[0].ToString())
                                                {
                                                    ds_ketqua.Tables["CN_TON"].Rows[i].Delete();
                                                }
                                            }
                                            ds_ketqua.AcceptChanges();
                                            copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[2]);
                                            ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            { }
                            #endregion
                        }
                    }
                    else
                    {
                        #region Uncheck
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            UncheckList(supernode, chk);
                        }
                        foreach (TreeViewItem supernode in trvBBAN.Items)
                        {
                            RefreshCheckBoxList(supernode);
                        }
                        if (m[0] == "NUTGOC")
                        {
                            ds_ketqua.Tables["CN_TON"].Rows.Clear();
                        }
                        // nếu click nút cha
                        if (m[1].ToString() == "NUTCHA")
                        {
                            #region Uncheck NUTCHA
                            if (ds_ketqua.Tables["CN_TON"].Rows.Count == 0) return;
                            for (int i = ds_ketqua.Tables["CN_TON"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["CN_TON"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["CN_TON"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                            #endregion
                        }
                        if (m[3].ToString() == "NUTCHA2")
                        {
                            #region Uncheck NUTCHA2
                            if (ds_ketqua.Tables["CN_TON"].Rows.Count == 0) return;
                            for (int i = ds_ketqua.Tables["CN_TON"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["CN_TON"].Rows[i]["SO_BBGIAO"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["CN_TON"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                            #endregion
                        }
                        if (m[4].ToString() == "NUTCON")
                        {
                            #region Uncheck NUTCON
                            if (ds_ketqua.Tables["CN_TON"].Rows.Count == 0) return;
                            for (int i = ds_ketqua.Tables["CN_TON"].Rows.Count - 1; i >= 0; i--)
                            {
                                if (ds_ketqua.Tables["CN_TON"].Rows[i]["MA_SOGCS"].ToString() == m[0].ToString())
                                {
                                    ds_ketqua.Tables["CN_TON"].Rows[i].Delete();
                                }
                            } //end of for loop     

                            ds_ketqua.AcceptChanges();
                            ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
            }
            catch(Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                MessageBox.Show(ex.Message.ToString());
                return;
            }
            this.Cursor = Cursors.Arrow;

        }

        private void copy_dataTable(DataTable dtnguon, DataTable dtDich, string ma_tngan)
        {
            try
            {
                DataRow[] rgINS = dtnguon.Select();
                for (int i = 0; i < rgINS.Length; i++)
                {
                    DataRow dr = dtDich.NewRow();
                    if (IN == 1)
                    {
                        dr["ID_HDON"] = rgINS[i]["ID_HDON"];
                        dr["TIEN_NOP"] = rgINS[i]["TIEN_PSINH"];
                        dr["THUE_NOP"] = rgINS[i]["THUE_PSINH"];
                        dr["SO_BBGIAO"] = rgINS[i]["SO_BBGIAO"];
                        dr["NGAY_CHAMNO"] = DBNull.Value;
                        dr["TTIEN"] = rgINS[i]["TONG_TIEN"];
                        dr["MA_TNGAN"] = rgINS[i]["MA_TNGAN"]; ;
                        dr["MA_SOGCS"] = rgINS[i]["MA_SOGCS"];
                        dr["MA_KHANG"] = rgINS[i]["MA_KHANG"];
                        dr["TEN_KHANG"] = rgINS[i]["TEN_KHANG"];
                        dr["DCHI_KHANG"] = rgINS[i]["DCHI_KHANG"];
                        dr["KY"] = rgINS[i]["KY"];
                        dr["THANG"] = rgINS[i]["THANG"];
                        dr["NAM"] = rgINS[i]["NAM"];

                    }
                    if (IN == 2)
                    {
                        dr["ID_HDON"] = rgINS[i]["ID_HDON"];
                        dr["TIEN_NOP"] = rgINS[i]["TIEN_NOP"];
                        dr["THUE_NOP"] = rgINS[i]["THUE_NOP"];
                        dr["SO_BBGIAO"] = rgINS[i]["SO_BBGIAO"];
                        dr["NGAY_CHAMNO"] = rgINS[i]["NGAY_CHAMNO"];
                        dr["THANG_HT"] = rgINS[i]["THANG_HT"];
                        dr["TTIEN"] = rgINS[i]["TONG_TIEN"];
                        dr["MA_TNGAN"] = rgINS[i]["MA_TNGAN"]; ;
                        dr["MA_SOGCS"] = rgINS[i]["MA_SOGCS"];
                        dr["MA_KHANG"] = rgINS[i]["MA_KHANG"];
                        dr["TEN_KHANG"] = rgINS[i]["TEN_KHANG"];
                        dr["DCHI_KHANG"] = rgINS[i]["DCHI_KHANG"];
                        dr["KY"] = rgINS[i]["KY"];
                        dr["THANG"] = rgINS[i]["THANG"];
                        dr["NAM"] = rgINS[i]["NAM"];
                    }
                    if (IN == 3)
                    {
                        dr["ID_HDON"] = rgINS[i]["ID_HDON"];
                        dr["TIEN_NOP"] = rgINS[i]["SO_TIEN"];
                        dr["THUE_NOP"] = rgINS[i]["TIEN_GTGT"];
                        dr["SO_BBGIAO"] = rgINS[i]["SO_BBGIAO"];
                        dr["NGAY_CHAMNO"] = rgINS[i]["NGAY_TTOAN"];
                        dr["THANG_HT"] = 1;
                        dr["TTIEN"] = rgINS[i]["TONGTIEN"];
                        dr["MA_TNGAN"] = ma_tngan;
                        dr["MA_SOGCS"] = rgINS[i]["MA_SOGCS"];
                        dr["MA_KHANG"] = rgINS[i]["MA_KHANG"];
                        dr["TEN_KHANG"] = rgINS[i]["TEN_KHANG"];
                        dr["DCHI_KHANG"] = rgINS[i]["DCHI_KHANG"];
                        dr["KY"] = rgINS[i]["KY"];
                        dr["THANG"] = rgINS[i]["THANG"];
                        dr["NAM"] = rgINS[i]["NAM"];
                    }
                    if (IN == 4)
                    {
                        dr["ID_HDON"] = rgINS[i]["ID_HDON"];
                        dr["TEN_KHANG"] = rgINS[i]["TEN_KHANG"];
                        dr["DCHI_KHANG"] = rgINS[i]["DCHI_KHANG"];
                        dr["MA_KHANG"] = rgINS[i]["MA_KHANG"];
                        dr["TIEN_NO"] = rgINS[i]["TIEN_NO"];
                        dr["THUE_NO"] = rgINS[i]["THUE_NO"];
                        dr["TONG_TIEN"] = rgINS[i]["TONG_TIEN"];
                        dr["SO_BBGIAO"] = rgINS[i]["SO_BBGIAO"];
                        dr["MA_SOGCS"] = rgINS[i]["MA_SOGCS"];
                        dr["KY"] = rgINS[i]["KY"];
                        dr["THANG"] = rgINS[i]["THANG"];
                        dr["NAM"] = rgINS[i]["NAM"];
                        dr["MA_TNGAN"] = rgINS[i]["MA_TNGAN"];
                    }
                    dtDich.Rows.Add(dr);
                    dtDich.AcceptChanges();
                }
            }
            catch(Exception ex)
            { }
        }

        private void UncheckList(TreeViewItem nodes, CheckBox chk)
        {
            //dt.Rows.Clear();
            if ((CheckBox)nodes.Header == chk)
            {
                UnCheckAllChildsWhenParentUnCheck(nodes);
                return;
            }
            foreach (TreeViewItem node in nodes.Items)
            {
                if (node.Header == chk)
                {
                    UnCheckParent(node);
                    if (node.Items.Count > 0)
                    {
                        foreach (TreeViewItem subNode in node.Items)
                        {
                            //check all Node
                            CheckBox chkNode = (CheckBox)subNode.Header;
                            chkNode.IsChecked = false;
                            if (subNode.Items.Count > 0)
                            {
                                UncheckList(subNode, chkNode);
                            }
                            //load database of childnode
                            //KiemTra_Insert_tbl_All_TheoDoiNo(chkChildNode);
                        }
                    }
                    else
                    {
                        CheckBox chkNode = (CheckBox)node.Header;
                        chkNode.IsChecked = false;
                    }
                }
                else
                {
                    UncheckList(node, chk);
                }

            }
        }

        private void UnCheckParent(TreeViewItem node)
        {
            TreeViewItem parentNode = node.Parent as TreeViewItem;
            if (parentNode != null)
            {
                CheckBox chkParentNode = (CheckBox)parentNode.Header;
                chkParentNode.IsChecked = false;
                UnCheckParent(parentNode);
            }
        }

        private void RefreshCheckBoxList(TreeViewItem nodes)
        {
            DataRow[] dr = null;
            foreach (TreeViewItem subNode in nodes.Items)
            {
                //check all Node
                CheckBox chkNode = (CheckBox)subNode.Header;
                if (subNode.Items.Count > 0)
                {
                    RefreshCheckBoxList(subNode);
                }
                else
                {
                    if (chkNode.IsChecked == true)
                    {
                        //if (tabTimKiem1.SelectedIndex == 0)
                        //{
                        //    if (cboTTrangCTo.SelectedIndex == 0)
                        //        dr = this.dsResult.Tables["CTO"].Select("SO_BBAN = '" + chkNode.Content.ToString() + "'");
                        //    else
                        //        //dr = this.dsResult.Tables["CTO"].Select("SO_BBAN = '" + chkNode.Content.ToString() + "'");

                        //        dr = this.dsResult.Tables["CTO"].Select("SO_BBAN_KDINH = '" + chkNode.Content.ToString() + "'");
                        //}
                        //else
                        //{
                        //    dr = this.dsResult.Tables["CTO"].Select("ID_BBAN_PBO = " + chkNode.Content.ToString() + "");
                        //    // lay thong tin ID phan bo
                        //    DataRow drBB = dsSearh.Tables["DD_BBAN_PBO"].NewRow();
                        //    drBB["MA_DVIQLY"] = strMaDViQLy;
                        //    drBB["ID_BBAN_PBO"] = Convert.ToInt64(chkNode.Content.ToString());
                        //    drBB["MA_DVIQLY_NHAN"] = dr[0]["MA_DVIQLY_NHAN"].ToString();
                        //    dsSearh.Tables["DD_BBAN_PBO"].Rows.Add(drBB);
                        //    dsSearh.AcceptChanges();
                        //}
                        //foreach (DataRow dro in dr)
                        //{
                        //    dt.ImportRow(dro);
                        //}
                    }
                    else
                    {
                        //if (tabTimKiem1.SelectedIndex == 1)
                        //{
                        //    //DataRow[] drDelete = null;
                        //    DataRow drDelete = dsSearh.Tables["DD_BBAN_PBO"].Select("ID_BBAN_PBO = " + chkNode.Content.ToString() + "").FirstOrDefault();
                        //    if (drDelete != null)
                        //    {
                        //        drDelete.Delete();
                        //    }
                        //    dsSearh.AcceptChanges();
                        //}
                    }
                }
            }
        }

        private void checkList(TreeViewItem nodes, CheckBox chk)
        {

            if ((CheckBox)nodes.Header == chk)
            {
                CheckAllChildsWhenParentCheck(nodes);
                return;
            }
            foreach (TreeViewItem node in nodes.Items)
            {
                if (node.Header == chk)
                {
                    if (node.Items.Count > 0)
                    {

                        foreach (TreeViewItem subNode in node.Items)
                        {
                            //check all Node
                            CheckBox chkNode = (CheckBox)subNode.Header;
                            chkNode.IsChecked = true;
                            if (subNode.Items.Count > 0)
                            {
                                checkList(subNode, chkNode); //chk);
                            }
                        }
                        CheckParentWhenAllChildChecked(nodes);
                    }
                    else
                    {

                        CheckParentWhenAllChildChecked(nodes);
                    }
                }
                else
                {
                    checkList(node, chk);
                }

            }
        }

        private void CheckParentWhenAllChildChecked(TreeViewItem nodes)
        {
            int count = 0;
            foreach (TreeViewItem node in nodes.Items)
            {
                CheckBox chkNode = (CheckBox)node.Header;
                if (chkNode.IsChecked == false)
                {
                    count = count + 1;
                    break;
                }
            }
            if (count == 0)
            {
                CheckBox chkNode = (CheckBox)nodes.Header;
                chkNode.IsChecked = true;
                TreeViewItem parentNode = nodes.Parent as TreeViewItem;
                if (parentNode != null)
                {

                    CheckParentWhenAllChildChecked(parentNode);
                }
            }
        }

        private void CheckAllChildsWhenParentCheck(TreeViewItem nodes)
        {
            foreach (TreeViewItem node in nodes.Items)
            {
                CheckBox chkNode = (CheckBox)node.Header;
                chkNode.IsChecked = true;
                CheckAllChildsWhenParentCheck(node);
            }
        }

        private void UnCheckAllChildsWhenParentUnCheck(TreeViewItem nodes)
        {
            foreach (TreeViewItem node in nodes.Items)
            {
                CheckBox chkNode = (CheckBox)node.Header;
                chkNode.IsChecked = false;
                UnCheckAllChildsWhenParentUnCheck(node);
            }
        }

        private void btnIn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.Wait;
                DataSet data = new DataSet();
                data = GlobalServices.Service_NPCUS.Ten_Tngan(strMadviqly, strUsername.ToUpper());
                string ten_tngan = "";
                if (data.Tables[0].Rows.Count > 0) ten_tngan = data.Tables[0].Rows[0]["TEN_TNGAN"].ToString();
                string strMatngan = strUsername;
                if (IN == 1)
                {
                    #region IN = 1
                    BaocaoNPC.Usercontrols.reports.RP_BC_TDN_ALL report = new RP_BC_TDN_ALL();
                    report.SetDataSource(ds_ketqua.Tables["NPC_CN"].DefaultView);
                    report.SetParameterValue("Ten_TNgan", ten_tngan);
                    report.SetParameterValue("THANGHT", dtpThangHTtim.SelectedDate.Value.Month);
                    report.SetParameterValue("NAMHT", dtpThangHTtim.SelectedDate.Value.Year);
                    report.SetParameterValue("THUNGAN", strUsername);
                    report.SetParameterValue("pTendienluc", strTen_dviqly);
                    report.SetParameterValue("pTencongty", strTen_dviqly_CT);
                    UtilReportOMS.RptManager.showReportToWin(this, report, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");

                    #endregion
                }
                if (IN == 2)
                {
                    #region IN = 2
                    if (dtpThangHTtim.SelectedDate.Value == null)
                    {
                        MessageBox.Show("Không có dữ liệu, vui lòng chọn lại!");
                        return;
                    }
                    else if (dtptungay.Text == string.Empty)
                    {
                        if (dtpdenngay.Text != string.Empty)
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = " NGAY_CHAMNO <= '" +dtpdenngay.SelectedDate.Value + "' and THANG_HT = " + dtpThangHTtim.SelectedDate.Value.Month;
                        else
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "THANG_HT = " + dtpThangHTtim.SelectedDate.Value.Month.ToString();
                    }
                    else if (dtptungay.Text != string.Empty && dtpdenngay.Text == string.Empty)
                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = " NGAY_CHAMNO >= '" + dtptungay.SelectedDate.Value + "' and THANG_HT = " + dtpThangHTtim.SelectedDate.Value.Month;
                    else if (dtptungay.Text != string.Empty && dtpdenngay.Text != string.Empty)
                    {
                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "NGAY_CHAMNO >= '" + dtptungay.SelectedDate.Value + "' and NGAY_CHAMNO <= '" + dtpdenngay.SelectedDate.Value + "' and THANG_HT = " + dtpThangHTtim.SelectedDate.Value.Month;
                    }
                    BaocaoNPC.Usercontrols.reports.RP_BC_TDN report = new RP_BC_TDN();
                    report.SetDataSource(ds_ketqua.Tables["NPC_CN"].DefaultView);
                    report.SetParameterValue("Ten_TNgan", ten_tngan);
                    report.SetParameterValue("THANGHT", dtpThangHTtim.SelectedDate.Value.Month);
                    if (dtptungay.Text != string.Empty)
                        report.SetParameterValue("TUNGAY", dtptungay.SelectedDate.Value.ToString("dd/MM/yyyy"));
                    else report.SetParameterValue("TUNGAY", "");
                    if (dtpdenngay.Text != string.Empty)
                        report.SetParameterValue("DENNGAY", dtpdenngay.SelectedDate.Value.ToString("dd/MM/yyyy"));
                    else report.SetParameterValue("DENNGAY", "");
                    report.SetParameterValue("NAMHT", dtpThangHTtim.SelectedDate.Value.Year);
                    report.SetParameterValue("THUNGAN", strUsername);
                    report.SetParameterValue("pTendienluc", strTen_dviqly);
                    report.SetParameterValue("pTencongty", strTen_dviqly_CT);
                    UtilReportOMS.RptManager.showReportToWin(this, report, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");

                    #endregion
                }
                if (IN == 3)
                {
                    #region IN == 3
                    if (dtptungay.Text == string.Empty)
                    {
                        if (dtpdenngay.Text != string.Empty)
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = " NGAY_CHAMNO <= '" + dtpdenngay.SelectedDate.Value ;
                        else
                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "ID_HDON > 0";
                    }
                    else if (dtptungay.Text != string.Empty && dtpdenngay.Text == string.Empty)
                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = " NGAY_CHAMNO >= '" + dtptungay.SelectedDate.Value ;
                    else if (dtptungay.Text != string.Empty && dtpdenngay.Text != string.Empty)
                    {
                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "NGAY_CHAMNO >= '" + dtptungay.SelectedDate.Value + "' and NGAY_CHAMNO <= '" + dtpdenngay.SelectedDate.Value+"'";
                    }
                    if (ds_ketqua != null && ds_ketqua.Tables["NPC_CN"].Rows.Count > 0)
                    {
                        BaocaoNPC.Usercontrols.reports.RP_DS_NopATM_QL report = new RP_DS_NopATM_QL();
                        report.SetDataSource(ds_ketqua.Tables["NPC_CN"].DefaultView);
                        report.SetParameterValue("ThuNgan", strMatngan);
                        report.SetParameterValue("Ten_TNgan", ten_tngan);
                        if (dtptungay.Text != string.Empty) 
                            report.SetParameterValue("TUNGAY", dtptungay.SelectedDate.Value.ToString("dd/MM/yyyy"));
                        else report.SetParameterValue("TUNGAY", "");
                        if (dtpdenngay.Text != string.Empty)
                            report.SetParameterValue("DENNGAY", dtpdenngay.SelectedDate.Value.ToString("dd/MM/yyyy"));
                        else report.SetParameterValue("DENNGAY", "");
                        report.SetParameterValue("THANGHT", dtpThangHTtim.SelectedDate.Value.Month);
                        report.SetParameterValue("NAMHT", dtpThangHTtim.SelectedDate.Value.Year);
                        report.SetParameterValue("pTendienluc", strTen_dviqly);
                        report.SetParameterValue("pTencongty", strTen_dviqly_CT);
                        UtilReportOMS.RptManager.showReportToWin(this, report, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                    }
                    else MessageBox.Show("Không có dữ liệu, vui lòng chọn lại!");
                    #endregion
                }

                if (IN == 4)
                {
                    #region IN == 4

                    if (ds_ketqua != null && ds_ketqua.Tables["CN_TON"].Rows.Count > 0)
                    {
                        BaocaoNPC.Usercontrols.reports.RP_DSTon_QL report = new RP_DSTon_QL();
                        report.SetDataSource(ds_ketqua.Tables["CN_TON"]);
                        report.SetParameterValue("ThuNgan", strMatngan);
                        report.SetParameterValue("Ten_TNgan", ten_tngan);
                        report.SetParameterValue("THANGHT", dtpThangHTtim.SelectedDate.Value.Month);
                        report.SetParameterValue("NAMHT", dtpThangHTtim.SelectedDate.Value.Year);
                        report.SetParameterValue("pTendienluc", strTen_dviqly);
                        report.SetParameterValue("pTencongty", strTen_dviqly_CT);
                        UtilReportOMS.RptManager.showReportToWin(this, report, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                    }
                    else MessageBox.Show("Không có dữ liệu, vui lòng chọn lại!");
                    #endregion
                }
                this.Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            { 
            }
        }

        private void dtptungay_GotFocus(object sender, RoutedEventArgs e)
        {

        }

        private void dtptungay_LostFocus(object sender, RoutedEventArgs e)
        {

        }

        private void dtpdenngay_GotFocus(object sender, RoutedEventArgs e)
        {

        }

        private void dtpdenngay_LostFocus(object sender, RoutedEventArgs e)
        {

        }

        private void RadDsachTheoBB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                IN = 2;
                txtGiaTri.Text = "";
                Cursor = Cursors.Wait;
                grdLich.IsEnabled = true;
                dtpThangHTtim.IsEnabled = true;
                ds_ketqua.Tables["NPC_CN"].Rows.Clear();
                DataSet ds = new DataSet();
                ds = GlobalServices.Service_NPCUS.SELECT_TNGAN_THEOBBGIAO(strMadviqly);
                if (ds == null) return;
                if (ds.Tables[0].Rows.Count == 0 || ds == null) return;
                #region Treeview
                TreeViewItem superNode = new TreeViewItem();
                TreeViewItem _node = new TreeViewItem();
                TreeViewItem _childnode = new TreeViewItem();
                CheckBox chkNode = new CheckBox();
                CheckBox chkChildNode = new CheckBox();
                if (trvBBAN != null) trvBBAN.Items.Clear();

                DataView dv = new DataView(ds.Tables[0]);
                dv.Sort = "MA_TNGAN";
                if (dv.Count == 0)
                {
                    superNode.Header = "Không có dữ liệu TNV";
                    superNode.Tag = "Không có dữ liệu TNV@";
                    trvBBAN.Items.Add(superNode);
                    return;
                }
                CheckBox chkSuperNode = new CheckBox();
                chkSuperNode.Content = strTen_dviqly;
                chkSuperNode.Tag = "NUTGOC" + "@" + "" + "@" + "" + "@" + "" + "@" + "";
                chkSuperNode.ToolTip = strTen_dviqly;
                chkSuperNode.IsChecked = false;
                chkSuperNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                superNode = new TreeViewItem();
                superNode.Header = chkSuperNode;
                superNode.IsExpanded = true;
                //superNode.IsSelected = true;
                //trvBBAN.Items.Add(superNode);
                string strnutcha = "";
                string strNutCon = "";
                string str_thungan = "";
                string str_ma_thungan = "";
                foreach (DataRowView dr in dv)
                {
                    str_ma_thungan = dr["MA_TNGAN"].ToString();
                    str_thungan = dr["TEN_TNGAN"].ToString();
                    #region "parentnode"
                    if (str_thungan != strnutcha)
                    {
                        chkNode = new CheckBox();
                        chkNode.Content = str_thungan;
                        chkNode.Tag = str_ma_thungan + "@NUTCHA" + "@" + "" + "@" + "" + "@" + "";
                        chkNode.ToolTip = str_thungan;
                        chkNode.IsChecked = false;
                        chkNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                        _node = new TreeViewItem();
                        _node.Header = chkNode;
                        _node.IsExpanded = false;
                        trvBBAN.Items.Add(_node);
                        strnutcha = str_thungan;

                        //So BBG
                        #region SoBBG
                        DataSet dsBBG = new DataSet();
                        dsBBG = GlobalServices.Service_NPCUS.SELECT_CHAMNO_OFFLINE_NUTCHA(strMadviqly,str_ma_thungan);
                        if (dsBBG != null && dsBBG.Tables[0].Rows.Count > 0)
                        {
                            if (dsBBG.Tables.Count > 0 && dsBBG.Tables[0].Rows.Count > 0)
                            {
                                TreeViewItem superNodeBB = new TreeViewItem();
                                TreeViewItem _nodeBB = new TreeViewItem();
                                //TreeViewItem _childnodeBB = new TreeViewItem();
                                //CheckBox chkNodeBB = new CheckBox();
                                CheckBox chkChildNodeBB = new CheckBox();

                                //this._node.Items.Clear(); // xoa tree trước khi đỗ dữ liệu mới
                                DataView dv2 = new DataView(dsBBG.Tables[0]);
                                dv2.Sort = "SO_BBGIAO, MA_SOGCS";
                                string strnutcha2 = "";
                                string str_bienban2 = "";
                                string str_ma_sogcs = "";
                                foreach (DataRowView dr2 in dv2)
                                {
                                    str_bienban2 = dr2["SO_BBGIAO"].ToString();
                                    str_ma_sogcs = dr2["MA_SOGCS"].ToString();
                                    #region "parentnode2"
                                    if (str_bienban2 != strnutcha2)
                                    {
                                        CheckBox chkNodeBB = new CheckBox();
                                        chkNodeBB.Content = str_bienban2;
                                        chkNodeBB.Tag = str_bienban2 + "@" + str_ma_thungan + "@" + "Biên bản" + "@NUTCHA2" + "@" + "";
                                        chkNodeBB.ToolTip = str_bienban2;
                                        chkNodeBB.IsChecked = false;
                                        chkNodeBB.Click += new RoutedEventHandler(this.CheckBox_Click);
                                        _nodeBB = new TreeViewItem();
                                        _nodeBB.Header = chkNodeBB;
                                        _nodeBB.IsExpanded = false;
                                        _node.Items.Add(_nodeBB);
                                        strnutcha2 = str_bienban2;
                                    }
                                    # endregion
                                    #region Ma So GCS

                                    TreeViewItem _nodeSo = new TreeViewItem();
                                    CheckBox chkChildNodeSo = new CheckBox();
                                    if (str_ma_sogcs != strNutCon)
                                    {
                                        CheckBox chkNodeSo = new CheckBox();
                                        chkNodeSo.Content = str_ma_sogcs;
                                        chkNodeSo.Tag = str_ma_sogcs + "@" + str_bienban2 + "@" + str_ma_thungan + "@" + "Sổ" + "@NUTCON";
                                        chkNodeSo.ToolTip = str_ma_sogcs;
                                        chkNodeSo.IsChecked = false;
                                        chkNodeSo.Click += new RoutedEventHandler(this.CheckBox_Click);
                                        _nodeSo = new TreeViewItem();
                                        _nodeSo.Header = chkNodeSo;
                                        _nodeSo.IsExpanded = false;
                                        _nodeBB.Items.Add(_nodeSo);
                                        strNutCon = str_ma_sogcs;
                                    }
                                    #endregion
                                }
                            }
                        }
                        #endregion
                    }
                    # endregion
                }
                #endregion
            }
            catch (Exception ex)
            {
                return;
            }
            finally
            { Cursor = Cursors.Arrow;}
        }

        private void RadDsachKhTonCuoithang_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                IN = 4;
                txtGiaTri.Text = "";
                Cursor = Cursors.Wait;
                dtpThangHTtim.IsEnabled = false;
                grdLich.IsEnabled = false;
                if (ds_TNV != null) ds_TNV.Clear();
                ds_TNV = GlobalServices.Service_NPCUS.SELECT_TNGAN_HD_TON(strMadviqly);
                //int count = ds_TNV.Tables[0].Rows.Count;
                ds_ketqua.Tables["CN_TON"].Rows.Clear();
                #region LOAD_TV
                try
                {
                    if (ds_TNV == null) return;
                    if (ds_TNV.Tables[0].Rows.Count == 0) return;
                    TreeViewItem superNode = new TreeViewItem();
                    TreeViewItem _node = new TreeViewItem();
                    TreeViewItem _childnode = new TreeViewItem();
                    CheckBox chkNode = new CheckBox();
                    CheckBox chkChildNode = new CheckBox();
                    CheckBox chkSuperNode = new CheckBox();
                    this.trvBBAN.Items.Clear();
                    DataView dv = new DataView(ds_TNV.Tables[0]);
                    dv.Sort = "MA_TNGAN";
                    if (dv.Count == 0) // không có dữ liệu không đưa ra tree
                    {
                        superNode.Header = "Không có dữ liệu TNV";
                        superNode.Tag = "Không có dữ liệu TNV@";
                        trvBBAN.Items.Add(superNode);
                        return;
                    }
                    chkSuperNode = new CheckBox();
                    chkSuperNode.Content = strTen_dviqly;
                    chkSuperNode.Tag = "NUTGOC" + "@" + "" + "@" + "" + "@" + "" + "@" + "";
                    chkSuperNode.ToolTip = strTen_dviqly;
                    chkSuperNode.IsChecked = false;
                    chkSuperNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                    superNode = new TreeViewItem();
                    superNode.Header = chkSuperNode;
                    superNode.IsExpanded = true;
                    //superNode.IsSelected = true;
                    //trvBBAN.Items.Add(superNode);
                    string strnutcha = "";
                    string strNutCon = "";
                    string str_thungan = "";
                    string str_ma_thungan = "";
                    foreach (DataRowView dr in dv)
                    {
                        str_ma_thungan = dr["MA_TNGAN"].ToString();
                        str_thungan = dr["TEN_TNGAN"].ToString();
                        #region "parentnode"
                        if (str_thungan != strnutcha)
                        {
                            chkNode = new CheckBox();
                            chkNode.Content = str_thungan;// +" -" + dr["NGAY_GIAO"].ToString();
                            chkNode.Tag = str_ma_thungan + "@NUTCHA" + "@" + "" + "@" + "" + "@" + "";
                            chkNode.ToolTip = str_thungan;
                            chkNode.IsChecked = false;
                            chkNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                            _node = new TreeViewItem();
                            _node.Header = chkNode;
                            _node.IsExpanded = false;
                            trvBBAN.Items.Add(_node);
                            strnutcha = str_thungan;

                            //So BBG
                            //#region SoBBG
                            //DataSet dsBBG = new DataSet();
                            //dsBBG = CHAMNO_OFFLINE_bus.LOAD_DSTon(str_ma_thungan, strMadviqly);
                            //if (dsBBG != null && dsBBG.Tables[0].Rows.Count >0)
                            //{
                            //    if (dsBBG.Tables.Count > 0 && dsBBG.Tables[0].Rows.Count > 0)
                            //    {
                            //        TreeViewItem superNodeBB = new TreeViewItem();
                            //        TreeViewItem _nodeBB = new TreeViewItem();
                            //        //TreeViewItem _childnodeBB = new TreeViewItem();
                            //        //CheckBox chkNodeBB = new CheckBox();
                            //        CheckBox chkChildNodeBB = new CheckBox();

                            //        // filter_treeDataSet();
                            //        //this._node.Items.Clear(); // xoa tree trước khi đỗ dữ liệu mới
                            //        DataView dv2 = new DataView(dsBBG.Tables[0]);
                            //        dv2.Sort = "SO_BBGIAO, MA_SOGCS";
                            //        string strnutcha2 = "";
                            //        string str_bienban2 = "";
                            //        string str_ma_sogcs = "";
                            //        foreach (DataRowView dr2 in dv2)
                            //        {
                            //            str_bienban2 = dr2["SO_BBGIAO"].ToString();
                            //            str_ma_sogcs = dr2["MA_SOGCS"].ToString();
                            //            //str_ma_sogcs = dr2["MA_SOGCS"].ToString();
                            //            if (IN != 1)
                            //            {
                            //                #region "parentnode2"
                            //                if (str_bienban2 != strnutcha2)
                            //                {
                            //                    CheckBox chkNodeBB = new CheckBox();
                            //                    chkNodeBB.Content = str_bienban2;
                            //                    chkNodeBB.Tag = str_bienban2 + "@" + str_ma_thungan + "@" + "Biên bản" + "@NUTCHA2" + "@" + "";
                            //                    chkNodeBB.ToolTip = str_bienban2;
                            //                    chkNodeBB.IsChecked = false;
                            //                    chkNodeBB.Click += new RoutedEventHandler(this.CheckBox_Click);
                            //                    _nodeBB = new TreeViewItem();
                            //                    _nodeBB.Header = chkNodeBB;
                            //                    _nodeBB.IsExpanded = false;
                            //                    _node.Items.Add(_nodeBB);
                            //                    strnutcha2 = str_bienban2;


                            //                }
                            //                # endregion
                            //                #region Ma So GCS

                            //                TreeViewItem _nodeSo = new TreeViewItem();
                            //                CheckBox chkChildNodeSo = new CheckBox();
                            //                if (str_ma_sogcs != strNutCon)
                            //                {
                            //                    CheckBox chkNodeSo = new CheckBox();
                            //                    chkNodeSo.Content = str_ma_sogcs;
                            //                    chkNodeSo.Tag = str_ma_sogcs + "@" + str_bienban2 + "@" + str_ma_thungan + "@" + "Sổ" + "@NUTCON";
                            //                    chkNodeSo.ToolTip = str_ma_sogcs;
                            //                    chkNodeSo.IsChecked = false;
                            //                    chkNodeSo.Click += new RoutedEventHandler(this.CheckBox_Click);
                            //                    _nodeSo = new TreeViewItem();
                            //                    _nodeSo.Header = chkNodeSo;
                            //                    _nodeSo.IsExpanded = false;
                            //                    _nodeBB.Items.Add(_nodeSo);
                            //                    strNutCon = str_ma_sogcs;
                            //                }
                            //                #endregion
                            //            }
                            //        }
                            //    }
                            //}

                            //#endregion
                        }
                        # endregion
                    }
                }
                catch (Exception ex)
                {
                    return;
                }
                #endregion
                Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            { }
        }

        private void RadDsachThuHo_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                IN = 3;
                txtGiaTri.Text = "";
                Cursor = Cursors.Wait;
                dtpThangHTtim.IsEnabled = false;
                grdLich.IsEnabled = true;
                ds_ketqua.Tables["NPC_CN"].Rows.Clear();
                DataSet ds = new DataSet();
                ds_TNV = GlobalServices.Service_NPCUS.SELECT_TNGAN_ATM(strMadviqly);
                if (ds_TNV == null) return;
                if (ds_TNV.Tables[0].Rows.Count == 0) return;
                TreeViewItem superNode = new TreeViewItem();
                TreeViewItem _node = new TreeViewItem();
                TreeViewItem _childnode = new TreeViewItem();
                CheckBox chkNode = new CheckBox();
                CheckBox chkChildNode = new CheckBox();
                if (trvBBAN != null) trvBBAN.Items.Clear();

                DataView dv = new DataView(ds_TNV.Tables[0]);
                dv.Sort = "MA_TNGAN";
                if (dv.Count == 0)
                {
                    superNode.Header = "Không có dữ liệu TNV";
                    superNode.Tag = "Không có dữ liệu TNV@";
                    trvBBAN.Items.Add(superNode);
                    return;
                }
                CheckBox chkSuperNode = new CheckBox();
                chkSuperNode.Content = strTen_dviqly;
                chkSuperNode.Tag = "NUTGOC" + "@" + "" + "@" + "" + "@" + "" + "@" + "";
                chkSuperNode.ToolTip = strTen_dviqly;
                chkSuperNode.IsChecked = false;
                chkSuperNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                superNode = new TreeViewItem();
                superNode.Header = chkSuperNode;
                superNode.IsExpanded = true;
                //trvBBAN.Items.Add(superNode);
                string strnutcha = "";
                string strNutCon = "";
                string str_thungan = "";
                string str_ma_thungan = "";
                foreach (DataRowView dr in dv)
                {
                    str_ma_thungan = dr["MA_TNGAN"].ToString();
                    str_thungan = dr["TEN_TNGAN"].ToString();
                    #region "parentnode"
                    if (str_thungan != strnutcha)
                    {
                        chkNode = new CheckBox();
                        chkNode.Content = str_thungan;// +" -" + dr["NGAY_GIAO"].ToString();
                        chkNode.Tag = str_ma_thungan + "@NUTCHA" + "@" + "" + "@" + "" + "@" + "";
                        chkNode.ToolTip = str_thungan;
                        chkNode.IsChecked = false;
                        chkNode.Click += new RoutedEventHandler(this.CheckBox_Click);
                        _node = new TreeViewItem();
                        _node.Header = chkNode;
                        _node.IsExpanded = false;
                        trvBBAN.Items.Add(_node);
                        strnutcha = str_thungan;
                    }
                    //So BBG
                    #region SoBBG
                    DataSet dsBBG = new DataSet();
                    dsBBG = GlobalServices.Service_NPCUS.SELECT_ATM_NUT_TNGAN(strMadviqly, str_ma_thungan);
                    int count = dsBBG.Tables[0].Rows.Count;
                    if (dsBBG != null && dsBBG.Tables[0].Rows.Count > 0)
                    {
                        if (dsBBG.Tables.Count > 0 && dsBBG.Tables[0].Rows.Count > 0)
                        {
                            TreeViewItem superNodeBB = new TreeViewItem();
                            TreeViewItem _nodeBB = new TreeViewItem();
                            CheckBox chkChildNodeBB = new CheckBox();
                            DataView dv2 = new DataView(dsBBG.Tables[0]);
                            dv2.Sort = "SO_BBGIAO, MA_SOGCS";
                            string strnutcha2 = "";
                            string str_bienban2 = "";
                            string str_ma_sogcs = "";
                            foreach (DataRowView dr2 in dv2)
                            {
                                str_bienban2 = dr2["SO_BBGIAO"].ToString();
                                str_ma_sogcs = dr2["MA_SOGCS"].ToString();
                                //str_ma_sogcs = dr2["MA_SOGCS"].ToString();

                                #region "parentnode2"
                                if (str_bienban2 != strnutcha2)
                                {
                                    CheckBox chkNodeBB = new CheckBox();
                                    chkNodeBB.Content = str_bienban2;
                                    chkNodeBB.Tag = str_bienban2 + "@" + str_ma_thungan + "@" + "Biên bản" + "@NUTCHA2" + "@" + "";
                                    chkNodeBB.ToolTip = str_bienban2;
                                    chkNodeBB.IsChecked = false;
                                    chkNodeBB.Click += new RoutedEventHandler(this.CheckBox_Click);
                                    _nodeBB = new TreeViewItem();
                                    _nodeBB.Header = chkNodeBB;
                                    _nodeBB.IsExpanded = false;
                                    _node.Items.Add(_nodeBB);
                                    strnutcha2 = str_bienban2;
                                }
                                # endregion
                                #region Ma So GCS

                                TreeViewItem _nodeSo = new TreeViewItem();
                                CheckBox chkChildNodeSo = new CheckBox();
                                if (str_ma_sogcs != strNutCon)
                                {
                                    CheckBox chkNodeSo = new CheckBox();
                                    chkNodeSo.Content = str_ma_sogcs;
                                    chkNodeSo.Tag = str_ma_sogcs + "@" + str_bienban2 + "@" + str_ma_thungan + "@" + "Sổ" + "@NUTCON";
                                    chkNodeSo.ToolTip = str_ma_sogcs;
                                    chkNodeSo.IsChecked = false;
                                    chkNodeSo.Click += new RoutedEventHandler(this.CheckBox_Click);
                                    _nodeSo = new TreeViewItem();
                                    _nodeSo.Header = chkNodeSo;
                                    _nodeSo.IsExpanded = false;
                                    _nodeBB.Items.Add(_nodeSo);
                                    strNutCon = str_ma_sogcs;
                                }
                                #endregion
                            }
                        }
                    }

                    #endregion
                    # endregion
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
            finally
            {
                Cursor = Cursors.Arrow;
            }
        }

        private void RadALLDSach_Click(object sender, RoutedEventArgs e)
        {
            IN = 1;
            dtpThangHTtim.IsEnabled = false;
            txtGiaTri.Text = "";
            Cursor = Cursors.Wait;
            grdLich.IsEnabled = false;
            ds_TNV = GlobalServices.Service_NPCUS.select_ThuNganVien(strMadviqly);
            ds_ketqua.Tables["NPC_CN"].Rows.Clear();
            fill_dulieu(ds_TNV);
            Cursor = Cursors.Arrow;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void Update_autotext()
        {
            try
            {
                if (ds_TNV == null) return;
                if (ds_TNV.Tables.Count == 0)
                {
                    txtGiaTri.ClearItem();
                    return;
                }
                DataTable dt = new DataTable();
                DataColumn Col1 = dt.Columns.Add("MA_TNGAN", typeof(String));
                dt.Columns.Add("TEN_TNGAN", typeof(String));
                var result = (from row in ds_TNV.Tables[0].AsEnumerable()
                             group row by new { MA_TNGAN = row.Field<string>("MA_TNGAN"), TEN_TNGAN = row.Field<string>("TEN_TNGAN")}   into grp
                              orderby grp.Key.MA_TNGAN
                              select new
                              {
                                  MA_TNGAN = grp.Key.MA_TNGAN,
                                  TEN_TNGAN = grp.Key.TEN_TNGAN,
                              }).ToList();
                for (int i = 0; i < result.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["MA_TNGAN"] = result[i].MA_TNGAN;
                    dr["TEN_TNGAN"] = result[i].TEN_TNGAN;
                    dt.Rows.Add(dr);
                    dt.AcceptChanges();
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.AcceptChanges();
                ProcessData(ds);
                txtGiaTri.ClearItem();
                if (ds != null) txtGiaTri.AddItem(new CMIS2_TextBox.AutoCompleteEntry(ds.Tables[0], "DataTable"));
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ProcessData(DataSet ds)
        {
            try
            {
                foreach (DataTable dt in ds.Tables)
                {
                    if (dt.Columns.Count > 0)
                    {
                        dt.Columns.AddRange(new DataColumn[] { new DataColumn("SEARCH", typeof(string)), new DataColumn("MA_SEARCH", typeof(string)), new DataColumn("TEN_SEARCHED", typeof(string)) });
                        foreach (DataRow dr in dt.Rows)
                        {
                            dr["SEARCH"] = dr["MA_TNGAN"].ToString() + "-" + dr["TEN_TNGAN"].ToString();
                            dr["MA_SEARCH"] = dr["SEARCH"];
                            dr["TEN_SEARCHED"] = dr["TEN_TNGAN"];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void txtGiaTri_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    if (txtGiaTri.Text.Trim() == "")
                    {
                        MessageBox.Show("Không có giá trị tìm kiếm!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                        return;
                    }
                    else
                    {
                        string[] m = txtGiaTri.Text.Split('-');
                        ds_TNV.Tables[0].DefaultView.RowFilter = "TEN_TNGAN = '" + m[1].ToUpper() + "'";
                        if (ds_TNV.Tables[0].DefaultView.Count == 0)
                        {
                            MessageBox.Show("Mã sổ không có giá trị tìm kiếm!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        Find(trvBBAN, m[1].ToUpper());
                        txtGiaTri.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Find(TreeView mainTreeView, string findToText)
        {
            //ds_Ketqua.Tables["CN"].Rows.Clear();
            foreach (object item in mainTreeView.Items)
            {
                TreeViewItem treeItem = mainTreeView.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

                if (treeItem != null)
                {
                    FindAll(treeItem, findToText);
                }
                if (item != null)
                {
                    //treeItem.IsExpanded = true;
                    CheckBox chkNode = (CheckBox)treeItem.Header;
                    string[] m = chkNode.Tag.ToString().Split('@');
                    // string obje = treeItem.Header.ToString();
                    if (isContains(chkNode.Content.ToString(), findToText))
                    {
                        chkNode.IsChecked = true;
                        switch(IN)
                        {
                            case 1:
                            #region NUTCHA
                                dsResult = GlobalServices.Service_NPCUS.SELECT_CHAMNO_OFFLINE_NUTCHA(strMadviqly, m[0]);
                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                {
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                                else
                                {
                                    for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                    {
                                        if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                        {
                                            ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                        }
                                    }
                                    ds_ketqua.AcceptChanges();
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                            }
                            #endregion
                            break;
                            case 2:
                            #region NUTCHA
                            // lấy dữ liệu của nút cha                               
                            dsResult = GlobalServices.Service_NPCUS.SELECT_CHAMNO_OFFLINE_NUTCHA(strMadviqly, m[0]);

                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                    {
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                    }
                                    else
                                    {
                                        for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                        {
                                            if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                            {
                                                ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                            }
                                        }
                                        ds_ketqua.AcceptChanges();
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[2]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                    }
                                }

                            }
                            #endregion
                            break;
                            case 3:
                            #region NUTCHA
                            // lấy dữ liệu của nút cha                               
                            dsResult = GlobalServices.Service_NPCUS.SELECT_ATM_NUT_TNGAN(strMadviqly, m[0]);

                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                {
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                                else
                                {
                                    for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                    {
                                        if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                        {
                                            ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                        }
                                    }
                                    ds_ketqua.AcceptChanges();
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                    ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                }
                            }
                            #endregion
                            break;
                            case 4:
                            #region NUTCHA
                            // lấy dữ liệu của nút cha                               
                            dsResult = GlobalServices.Service_NPCUS.SELECT_HDON_TON_NUTTNGAN(strMadviqly, m[0]);

                            if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                            {
                                if (ds_ketqua.Tables["CN_TON"].Rows.Count == 0)
                                {
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[0]);
                                    ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                }
                                else
                                {
                                    for (int i = ds_ketqua.Tables["CN_TON"].Rows.Count - 1; i >= 0; i--)
                                    {
                                        if (ds_ketqua.Tables["CN_TON"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                        {
                                            ds_ketqua.Tables["CN_TON"].Rows[i].Delete();
                                        }
                                    }
                                    ds_ketqua.AcceptChanges();
                                    copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[0]);
                                    ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                }
                            }
                            #endregion
                            break;
                        }
                    }
                }
            }
        }

        void FindAll(ItemsControl items, string findToText)
        {
            foreach (object obj in items.Items)
            {
                ItemsControl childControl = items.ItemContainerGenerator.ContainerFromItem(obj) as ItemsControl;
                if (childControl != null)
                {
                    FindAll(childControl, findToText);
                }
                TreeViewItem item = childControl as TreeViewItem;

                if (item != null)
                {
                    CheckBox chkNode = (CheckBox)item.Header;
                    string[] m = chkNode.Tag.ToString().Split('@');
                    if (isContains(chkNode.Content.ToString(), findToText))
                    {
                        chkNode.IsChecked = true;
                        switch (IN)
                        {
                            case 1:
                                #region NUTCHA
                                dsResult = GlobalServices.Service_NPCUS.SELECT_CHAMNO_OFFLINE_NUTCHA(strMadviqly, m[0]);
                                if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                    {
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                    }
                                    else
                                    {
                                        for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                        {
                                            if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                            {
                                                ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                            }
                                        }
                                        ds_ketqua.AcceptChanges();
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                    }
                                }
                                #endregion
                                break;
                            case 2:
                                #region NUTCHA
                                // lấy dữ liệu của nút cha                               
                                dsResult = GlobalServices.Service_NPCUS.SELECT_CHAMNO_OFFLINE_NUTCHA(strMadviqly, m[0]);

                                if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (dsResult.Tables[0].Rows.Count > 0)
                                    {
                                        if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                        {
                                            copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                        }
                                        else
                                        {
                                            for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                            {
                                                if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                                {
                                                    ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                                }
                                            }
                                            ds_ketqua.AcceptChanges();
                                            copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[2]);
                                            ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                        }
                                    }

                                }
                                #endregion
                                break;
                            case 3:
                                #region NUTCHA
                                // lấy dữ liệu của nút cha                               
                                dsResult = GlobalServices.Service_NPCUS.SELECT_ATM_NUT_TNGAN(strMadviqly, m[0]);

                                if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (ds_ketqua.Tables["NPC_CN"].Rows.Count == 0)
                                    {
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                    }
                                    else
                                    {
                                        for (int i = ds_ketqua.Tables["NPC_CN"].Rows.Count - 1; i >= 0; i--)
                                        {
                                            if (ds_ketqua.Tables["NPC_CN"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                            {
                                                ds_ketqua.Tables["NPC_CN"].Rows[i].Delete();
                                            }
                                        }
                                        ds_ketqua.AcceptChanges();
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["NPC_CN"], m[0]);
                                        ds_ketqua.Tables["NPC_CN"].DefaultView.RowFilter = "1=1";
                                    }
                                }
                                #endregion
                                break;
                            case 4:
                                #region NUTCHA
                                // lấy dữ liệu của nút cha                               
                                dsResult = GlobalServices.Service_NPCUS.SELECT_HDON_TON_NUTTNGAN(strMadviqly, m[0]);

                                if (dsResult != null && dsResult.Tables[0].Rows.Count > 0)
                                {
                                    if (ds_ketqua.Tables["CN_TON"].Rows.Count == 0)
                                    {
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[0]);
                                        ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                    }
                                    else
                                    {
                                        for (int i = ds_ketqua.Tables["CN_TON"].Rows.Count - 1; i >= 0; i--)
                                        {
                                            if (ds_ketqua.Tables["CN_TON"].Rows[i]["MA_TNGAN"].ToString() == m[0].ToString())
                                            {
                                                ds_ketqua.Tables["CN_TON"].Rows[i].Delete();
                                            }
                                        }
                                        ds_ketqua.AcceptChanges();
                                        copy_dataTable(dsResult.Tables[0], ds_ketqua.Tables["CN_TON"], m[0]);
                                        ds_ketqua.Tables["CN_TON"].DefaultView.RowFilter = "1=1";
                                    }
                                }
                                #endregion
                                break;
                        }
                    }
                }
            }
        }

        private bool isContains(string a, string b)
        {
            if (a.ToUpper().Trim() == b.ToUpper().Trim())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

}


